/*
 * Copyright 2025 Signal Messenger, LLC
 * SPDX-License-Identifier: AGPL-3.0-only
 */

package org.signal.glide;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glide_custom_model);

        ImageView localImageView = findViewById(R.id.local_image);
        ImageView remoteImageView = findViewById(R.id.remote_image);
        ImageView assetImageView = findViewById(R.id.asset_image);

        // 加载本地图片, 先把图片放到context.getFilesDir()目录下
        Glide.with(this)
            .load(new CustomImageModel("test_local.jpg", CustomImageModel.ImageType.LOCAL))
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(GlideException e, Object model, Target target, boolean isFirstResource) {
                    Log.d(TAG, "Failed to load local image", e);
                    Toast.makeText(MainActivity.this, "Failed to load local image", Toast.LENGTH_SHORT).show();
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
                    Log.d(TAG, "Successfully loaded local image");
                    return false;
                }
            })
            .into(localImageView);

        // 加载远程图片
        Glide.with(this)
            .load(new CustomImageModel("https://img0.baidu.com/it/u=4172295007,3022602453&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=667", CustomImageModel.ImageType.REMOTE))
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(GlideException e, Object model, Target target, boolean isFirstResource) {
                    Log.e(TAG, "Failed to load remote image", e);
                    Toast.makeText(MainActivity.this, "Failed to load remote image", Toast.LENGTH_SHORT).show();
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
                    Log.d(TAG, "Successfully loaded remote image");
                    return false;
                }
            })
            .into(remoteImageView);

        // 加载 Asset 图片
        Glide.with(this)
            .load(new CustomImageModel("test_local.jpg", CustomImageModel.ImageType.ASSET))
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(GlideException e, Object model, Target target, boolean isFirstResource) {
                    Log.e(TAG, "Failed to load asset image", e);
                    Toast.makeText(MainActivity.this, "Failed to load asset image", Toast.LENGTH_SHORT).show();
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
                    Log.d(TAG, "Successfully loaded asset image");
                    return false;
                }
            })
            .into(assetImageView);
    }
} 